package com.andnnl.groupby;

import com.google.common.collect.Lists;

import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;


public class ReduceTest2 {

    public static void main(String[] args) throws Exception {
        String[] types={"san","nas"};


        List<Foo> fooList = Lists.newArrayList(
                new Foo("A", "san", 1.0, 2),
                new Foo("A", "nas", 13.0, 1),
                new Foo("B", "san", 112.0, 3),
                new Foo("C", "san", 43.0, 5),
                new Foo("B", "nas", 77.0, 7)
        );
        Map sumByName = fooList.stream().collect(
                Collectors.groupingBy((Foo p) -> p.getName()
                        , Collectors.summingLong((Foo p) -> p.getCount()))
        );
        System.out.println(sumByName);
        long t1;
        long t2;


        // 统计并行执行list的线程
        Set<Thread> threadSet = new CopyOnWriteArraySet<>();
        // 并行执行
        fooList.parallelStream().forEach(integer -> {
            Thread thread = Thread.currentThread();
            // System.out.println(thread);
            // 统计并行执行list的线程
            threadSet.add(thread);
        });
        System.out.println("threadSet一共有" + threadSet.size() + "个线程");
        System.out.println("系统一个有"+Runtime.getRuntime().availableProcessors()+"个cpu");

//        t1=System.currentTimeMillis();
//        fooList.stream()
//                .collect(Collectors.groupingBy(Foo::getName, Collectors.toList()))
//                .forEach(
//                        (name, fooListByName) -> {
//                            System.out.printf("%s,%s \n",name,fooListByName.stream().mapToInt(a -> a.getCount()).sum());
//                        }
//                );
//        t2=System.currentTimeMillis();
//        System.out.println(t2-t1);


        //非并行
        t1 = System.currentTimeMillis();
        Map<String, Long> sumByName2 = fooList.stream().collect(
                Collectors.groupingBy(Foo::getName, Collectors.summingLong(Foo::getCount))
        );
        t2 = System.currentTimeMillis();
        System.out.println(sumByName2);
        System.out.println(t2 - t1);

        t1=System.currentTimeMillis();
        //多线程并行计算
        Map<String,Long> sumByName3 = fooList.parallelStream().collect(
                Collectors.groupingByConcurrent(Foo::getName, Collectors.summingLong(Foo::getCount))
        );
        t2=System.currentTimeMillis();
        System.out.println(sumByName3);
        System.out.println(t2-t1);


//        //串行分组
//        t1 = System.currentTimeMillis();
//        Map<String, List<Foo>> males = fooList.stream()
//                .collect(
//                        Collectors.groupingBy(Foo::getName));
//        t2=System.currentTimeMillis();
//        System.out.println(t2-t1);
//
//        //并行分组
//        t1 = System.currentTimeMillis();
//        Map<String, List<Foo>> males2 = fooList.parallelStream()
//                .collect(
//                        Collectors.groupingByConcurrent(Foo::getName));
//        t2=System.currentTimeMillis();


//        System.out.println(t2-t1);

//        List<Bar> barList = Lists.newArrayList();
//        fooList
//                .stream()
//                .collect(Collectors.groupingBy(Foo::getName,Collectors.toList()))
//                .forEach((name,fooListByName)->{
//                    Bar bar = new Bar();
//                    bar = fooListByName
//                            .stream()
//                            .reduce(bar,(u,t)->u.sum(t),(u,t)->u);
//                    System.out.println(bar.toString());
//                    barList.add(bar);
//                });


    }//end function
    /*
    输出结果
    name:A
    count:3
    totalTypeValue:14.0
    bazList:
        type:san
        typeValue:1.0
        type:nas
        typeValue:13.0

    name:B
    count:10
    totalTypeValue:189.0
    bazList:
        type:san
        typeValue:112.0
        type:nas
        typeValue:77.0

    name:C
    count:5
    totalTypeValue:43.0
    bazList:
        type:san
        typeValue:43.0
    */
}